package com.sen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.DrugtypeEntity;
import com.sen.service.DrugtypeService;
import com.sen.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 药品类型表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("drugtype")
@Slf4j
public class DrugtypeController {
    @Autowired
    private DrugtypeService drugtypeService;


    /**
     * 查询带子类的所有药品类型
     * @param drugtype 包含药品类型所有信息
     * @return
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String,String> drugtype){
        log.info("接收数据为{}",drugtype);
        List<DrugtypeEntity> drugtypeEntities = drugtypeService.queryAllDrugType(drugtype);
        return R.ok().put("drugtypeEntities", drugtypeEntities);
    }

    /**
     * 单独查询所有类型一条一条列出来
     * @return
     */
    @RequestMapping("/queryList")
    public R queryList(){
        List<DrugtypeEntity> drugtypeEntities = drugtypeService.list();
        log.info("drugtypeEntities:[{}]", drugtypeEntities);
        log.debug("输出DEBUG级别日志");
        log.info("输出INFO级别日志");
        log.warn("输出WARN级别日志");
        log.error("输出ERROR级别日志");
        return R.ok().put("drugtypeEntities", drugtypeEntities);
    }
    /**
     * 添加药品类别
     */
    @PostMapping("/addDrugType")
    public R addDrugType(DrugtypeEntity drugtypeEntity){
        System.out.println("---------------------"+drugtypeEntity);
        DrugtypeEntity one = drugtypeService.getOne(new QueryWrapper<DrugtypeEntity>().eq("tid", drugtypeEntity.getPid()).select("type"));
        drugtypeEntity.setType(one.getType()+1);
        drugtypeService.save(drugtypeEntity);
        return R.ok();
    }
    //修改
    @PostMapping("/updateDrugType")
    public R updateDrugType(Integer tid,String name,Integer pid){
        drugtypeService.update(new UpdateWrapper<DrugtypeEntity>().eq("tid", tid).set("name", name).set("pid", pid));
        return R.ok();
    }
    //修改
    @PostMapping("/drugTypeById")
    public R drugTypeById(Integer tid){
        DrugtypeEntity drugtype = drugtypeService.getById(tid);
        return R.ok().put("drugtype",drugtype);
    }
}
